package com.tfy.liquor.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tfy.liquor.bean.MyPage;
import com.tfy.liquor.bean.SystemConfiguration;
import com.tfy.liquor.dao.SystemConfigurationDao;
import com.tfy.liquor.service.SystemConfigurationService;
import com.tfy.liquor.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class SystemConfigurationServiceImpl implements SystemConfigurationService {
    @Autowired
    private SystemConfigurationDao systemConfigurationDao;

    // 添加信息
    @Override
    public Boolean addSystemConfiguration(SystemConfiguration systemConfiguration) {
        // 获取当前时间为创建时间
        systemConfiguration.setCreateTime(DateUtil.getNowDateTime());
        return systemConfigurationDao.addSystemConfiguration(systemConfiguration) > 0;
    }

    // 根据id获取信息
    @Override
    public SystemConfiguration getSystemConfigurationById(Integer id) {
        return systemConfigurationDao.getSystemConfigurationById(id);
    }

    // 根据title获取信息
    @Override
    public SystemConfiguration getSystemConfigurationByTitle(String title) {
        return systemConfigurationDao.getSystemConfigurationByTitle(title);
    }

    // 修改使用状态
    @Override
    public Boolean editSystemConfigurationStatus(SystemConfiguration systemConfiguration) {
        return systemConfigurationDao.editSystemConfigurationStatus(systemConfiguration) > 0;
    }

    // 获取系统配置信息
    @Override
    public MyPage getSystemConfiguration(Integer pageNum, Integer pageSize) {
        // 如果传入的参数是负数，则默认为1
        pageNum = Math.max(pageNum, 1);
        pageSize = pageSize < 1 ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<SystemConfiguration> list = systemConfigurationDao.getSystemConfiguration();
        // 可以将结果转换为 Page ,
        return new MyPage((Page) list);
    }
}
